//
//  FKBusinessGiftBagVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/4/3.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKBusinessGiftBagVC.h"
#import "FKBusinessGiftBagCell.h"
#import "FKCommodityDetailVC.h"
#import "FKGiftBagListVC.h"
#import <UIButton+WebCache.h>
#import "FKPosterVC.h"

@interface FKBusinessGiftBagVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, weak) UIButton *floatBtn;
@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, weak) UIImageView *header;
@property (nonatomic, weak) UIImageView *footer;

@property (nonatomic, copy) NSString *posterUrl;
@property (nonatomic, copy) NSString *goodsUrl;

@end

@implementation FKBusinessGiftBagVC

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"创业礼包";
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, fkScreenH - fkStatusAndNaBarH) style:UITableViewStyleGrouped];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate = self;
    
    UIImageView *tagView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wenzi"]];
    [self.view addSubview:tagView];
    
    UIButton *floatBtn = [UIButton fk_btnWithTarget:self action:@selector(floatBtnClicked) img:nil];
    [self.view addSubview:floatBtn];
    self.floatBtn = floatBtn;
    [floatBtn fk_viewCornerRadius:fk_adjustW(30) borderWidth:2 borderColor:[UIColor whiteColor]];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIImageView *header = [[UIImageView alloc] init];
    self.tableView.tableHeaderView = header;
    self.header = header;
    
    UIImageView *footer = [[UIImageView alloc] init];
    self.tableView.tableFooterView = footer;
    self.footer = footer;
    footer.userInteractionEnabled = true;
    
    UIButton *moreBtn = [UIButton fk_btnWithTarget:self action:@selector(moreBtnClicked) img:[UIImage imageNamed:@"chakangengduo"]];
    [footer addSubview:moreBtn];
    [moreBtn fk_viewCornerRadius:fk_adjustW(12) borderWidth:0 borderColor:nil];
    
    [floatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(fk_adjustW(60));
        make.right.offset(-20);
        make.bottom.offset(-fk_adjustW(80));
    }];
    [tagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(floatBtn.mas_centerY);
        make.centerX.equalTo(floatBtn);
        make.width.offset(fk_adjustW(90));
        make.height.offset(fk_adjustW(50));
    }];
    [moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.offset(fk_adjustW(24));
//        make.width.offset(fk_adjustW(115));
        make.top.offset(fk_adjustW(10));
        make.centerX.offset(0);
    }];
    
    [self loadData];
}

- (void)floatBtnClicked
{
    [FKLoginTool checkLogin:^{
        if (self.posterUrl && self.goodsUrl) {
            FKPosterVC *vc = [[FKPosterVC alloc] init];
            vc.imageUrl = self.posterUrl;
            vc.qrcodeUrl = self.goodsUrl;
            [self presentViewController:vc animated:true completion:nil];
        }
    }];
}

- (void)moreBtnClicked
{
    FKGiftBagListVC *vc = [[FKGiftBagListVC alloc] init];
    vc.title = self.title;
    fkPushNav(vc);
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKBusinessGiftBagCell *cell = [FKBusinessGiftBagCell cellWith:tableView];
    cell.data = self.datas[indexPath.section];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return fk_adjustW(185);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
//    if (section == 0) {
//        return 0.01;
//    }
//    return 5;
    if (section == 0) {
        return 5;
    }
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
//    return 0.01;
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKCommodityDetailVC *vc = [[FKCommodityDetailVC alloc] init];
    NSDictionary *data = self.datas[indexPath.section];
    vc.itemId = data[@"id"];
    if (!self.isActive) { // 不是专题活动，是创业礼包
        vc.package = true;
    }
    fkPushNav(vc);
}

#pragma mark - 网络请求
- (void)loadData
{
    NSString *suffix = @"m=Home&c=Item&a=set_meal"; // 创业礼包
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if (self.isActive) {
        suffix = @"m=Home&c=Item&a=themeActivity"; // 主题活动
        param[@"item_cate_id"] = self.item_cate_id;
    }
    [FKHttpTool postWithSuffix:suffix param:param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            NSDictionary *header = result.data[@"head"];
            NSDictionary *footer = result.data[@"foot"];
            NSDictionary *suspension = result.data[@"suspension"];
            self.posterUrl = suspension[@"content"];
            self.goodsUrl = result.data[@"set_meal_url"];
            
            [self.header sd_setImageWithURL:[NSURL URLWithString:header[@"content"]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                CGFloat headerH = fkScreenW * image.size.height / image.size.width;
                self.header.frame = CGRectMake(0, 0, fkScreenW, headerH);
                self.tableView.tableHeaderView = self.header;
            }];
            [self.footer sd_setImageWithURL:[NSURL URLWithString:footer[@"content"]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                CGFloat footerH = fkScreenW * image.size.height / image.size.width;
                self.footer.frame = CGRectMake(0, 0, fkScreenW, footerH);
                self.tableView.tableFooterView = self.footer;
            }];
            [self.floatBtn sd_setImageWithURL:[NSURL URLWithString:suspension[@"content"]] forState:UIControlStateNormal];
            
            self.datas = result.data[@"set_meal"];
            [self.tableView reloadData];
            
            self.tableView.backgroundColor = [UIColor fk_colorWithString:result.data[@"background_color"]];
        }
    } failure:nil];
}


@end
